#include <iostream>
#include <fstream>
#include <vector>

#include "MapEnvironment.h"
#include "vect.h"

MapEnvironment::MapEnvironment(const std::string& filename) {
	loadMap(filename);
}

void MapEnvironment::loadMap(const std::string& mapname) {
	std::ifstream game(mapname.c_str());
	int width;
	int height;
	std::string type;
	int dummy;
	game >> type >> width >> height >> dummy;
	game.get();

	gamemap.clear();

	if (type == "P2") { // ASCII numbers
		gamemap.empty();
		for (int i = 0; i < height; ++i) {
			gamemap.push_back(std::vector<char>());
			for (int j = 0; j < width; ++j) {
				gamemap[i].push_back(game.get());
			}
		}
	} else {
		if (type == "P5") { // BINARY bytes
			gamemap.empty();
			for (int i = 0; i < height; ++i) {
				gamemap.push_back(std::vector<char>());
				for (int j = 0; j < width; ++j) {
					gamemap[i].push_back(game.get());
				}
			}
		} else {
			std::cerr << "FAILED TO LOAD MAP" << std::endl;
			exit(1);
		}

	}

}

void MapEnvironment::think() {

}

void MapEnvironment::draw() {
	glBegin(GL_LINES);
	glColor3d(0,0,1);
	for (int i = -20; i <= 20; ++i) {

		glVertex3dv(vect(16 *  20, 16 *   i, -16));
		glVertex3dv(vect(16 * -20, 16 *   i, -16));
		glVertex3dv(vect(16 *   i, 16 *  20, -16));
		glVertex3dv(vect(16 *   i, 16 * -20, -16));

	}

	glEnd();
}
